#include "WordLibrary.h"
#include "Random.h"
using namespace std;

words_lib WordsLibrary;

bool AddWord(std::wstring kana, std::wstring romaji, WordType type)
{
	word w;
	w.kana = kana;
	w.romaji = romaji;
	switch (type)
	{
	case WT_HIRAGANA:
		WordsLibrary.hiragana.push_back(w);
		break;
	case WT_KATAKANA:
		WordsLibrary.katakana.push_back(w);
		break;
	default:
		return false;
	}
	return true;
}

#include <list>

namespace
{
	list<size_t> hira_was;
	list<size_t> kata_was;
	bool is_hira_was(size_t n)
	{
		list<size_t>::iterator p;
		for (p = hira_was.begin(); p != hira_was.end(); p++)
		{
            if (*p == n)
				return true;
		}
		return false;
	}
	bool is_kata_was(size_t n)
	{
		list<size_t>::iterator p;
		for (p = kata_was.begin(); p != kata_was.end(); p++)
		{
            if (*p == n)
				return true;
		}
		return false;
	}
	void clear_mem()
	{
		hira_was.clear();
		kata_was.clear();
	}
}

bool GetRandomWord(WordType type, word &w)
{
	if (WordsLibrary.hiragana.empty() && WordsLibrary.katakana.empty())
		return false;
	bool can_hira = true, can_kata = true;
	switch (type)
	{
	case WT_BOTH:
		break;
	case WT_KATAKANA:
		can_hira = false;
		break;
	case WT_HIRAGANA:
		can_kata = false;
		break;
	}
	while (true)
	{
		bool kata = false, hira = false;
		if (kata_was.size() == WordsLibrary.katakana.size() || WordsLibrary.katakana.empty())
			can_kata = false;
		if (hira_was.size() == WordsLibrary.hiragana.size() || WordsLibrary.hiragana.empty())
			can_hira = false;
		if (!can_hira && !can_kata)
			return false;
		if (can_kata)
		{
			if (can_hira)
				kata = random_p(random_range((int) 30, (int) 80));
			else
				kata = true;
		}
		if (can_hira && !kata)
		{
            hira = true;
		}

		if (kata)
		{
			size_t sw = kata_was.size();
			size_t sl = WordsLibrary.katakana.size();
			if (sw == sl || WordsLibrary.katakana.empty())
			{
				can_kata = false;
				continue;
			}
			size_t r = random_range((size_t) 0, (size_t) WordsLibrary.katakana.size());
			size_t r_was = r;
			while (is_kata_was(r))
			{
                r++;
                if (r >= WordsLibrary.katakana.size())
					r = 0;
				if (r == r_was)
				{
					can_kata = false;
					break;
				}
			}
            if (!can_kata)
				continue;
			w = WordsLibrary.katakana[r];
			kata_was.push_back(r);
			break;
		}
		else if (hira)
		{
			size_t sw = hira_was.size();
			size_t sl = WordsLibrary.hiragana.size();
			if (sw == sl || WordsLibrary.hiragana.empty())
			{
				can_hira = false;
				continue;
			}
			size_t r = random_range((size_t) 0, (size_t) WordsLibrary.hiragana.size());
			size_t r_was = r;
			while (is_hira_was(r))
			{
                r++;
                if (r >= WordsLibrary.hiragana.size())
					r = 0;
				if (r == r_was)
				{
					can_hira = false;
					break;
				}
			}
            if (!can_hira)
				continue;
			w = WordsLibrary.hiragana[r];
			hira_was.push_back(r);
			break;
		}
		else
			return false;
	}
	return true;
}

void ResetWordsRandom()
{
	clear_mem();
}
